<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\logic\Announcement as CommonAnnouncement;

/**
 * 公告管理逻辑
 */
class Announcement extends AdminBase
{
    
    public static $commonAnnouncementLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonAnnouncementLogic) && static::$commonAnnouncementLogic = new CommonAnnouncement();
    }
    
    /**
     * 获取公告列表搜索条件
     * @param array $data 搜索参数
     * @return array
     */
    public function getWhere($data = [])
    {
        return static::$commonAnnouncementLogic->getWhere($data);
    }
    
    /**
     * 获取公告列表（后台专用）
     */
    public function getAnnouncementListForAdmin($where = [], $field = true, $order = 'sort DESC, publish_time DESC, id DESC', $paginate = DB_LIST_ROWS)
    {
        return static::$commonAnnouncementLogic->getAnnouncementListForAdmin($where, $field, $order, $paginate);
    }
    
    /**
     * 获取公告信息
     */
    public function getAnnouncementInfo($where = [], $field = true)
    {
        $result = static::$commonAnnouncementLogic->getAnnouncementInfo($where, $field);
        
        // common层返回格式为 [状态码, 消息, 数据]，取第三个元素作为数据
        return ($result[0] == RESULT_SUCCESS) ? $result[2] : [];
    }
    
    /**
     * 获取公告类型选项
     * @return array
     */
    public function getTypeOptions()
    {
        return static::$commonAnnouncementLogic->getTypeOptions();
    }
    
    /**
     * 获取公告状态选项
     * @return array
     */
    public function getStatusOptions()
    {
        return static::$commonAnnouncementLogic->getStatusOptions();
    }
    
    /**
     * 添加/编辑公告
     */
    public function announcementEdit($data = [])
    {
        $url = url('announcementList');
        
        // 验证必填字段
        if (empty($data['title'])) {
            return [RESULT_ERROR, '公告标题不能为空'];
        }
        
        if (empty($data['content'])) {
            return [RESULT_ERROR, '公告内容不能为空'];
        }
        
        // 验证类型是否有效
        $typeOptions = $this->modelAnnouncement->getTypeOptions();
        $type = $data['type'] ?? 1;
        if (!isset($typeOptions[$type])) {
            return [RESULT_ERROR, '公告类型无效'];
        }
        
        // 设置默认值
        $data['type'] = $type;
        $data['status'] = $data['status'] ?? $this->modelAnnouncement::STATUS_DRAFT;
        $data['sort'] = $data['sort'] ?? 0;
        
        if (!empty($data['id'])) {
            // 编辑公告
            $result = static::$commonAnnouncementLogic->editAnnouncement($data);
            $action = '编辑';
        } else {
            // 添加公告
            $result = static::$commonAnnouncementLogic->addAnnouncement($data);
            $action = '新增';
        }
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log($action, $action . '公告，标题：' . $data['title']);
            return [RESULT_SUCCESS, $result[1], $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 删除公告
     */
    public function announcementDel($data = [])
    {
        $url = url('announcementList');
        
        if (empty($data['id'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonAnnouncementLogic->deleteAnnouncement($data);
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log('删除', '删除公告，id：' . $data['id']);
            return [RESULT_SUCCESS, $result[1], $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 发布公告
     */
    public function publishAnnouncement($data = [])
    {
        $url = url('announcementList');
        
        if (empty($data['id'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonAnnouncementLogic->publishAnnouncement($data);
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log('发布', '发布公告，id：' . $data['id']);
            return [RESULT_SUCCESS, $result[1], $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 下线公告
     */
    public function offlineAnnouncement($data = [])
    {
        $url = url('announcementList');
        
        if (empty($data['id'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonAnnouncementLogic->offlineAnnouncement($data);
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log('下线', '下线公告，id：' . $data['id']);
            return [RESULT_SUCCESS, $result[1], $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 设置公告排序
     */
    public function setAnnouncementSortOrder($data = [])
    {
        $url = url('announcementList');
        
        if (empty($data['id']) || !isset($data['sort'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $updateData = [
            'id' => $data['id'],
            'sort' => intval($data['sort'])
        ];
        
        $result = static::$commonAnnouncementLogic->editAnnouncement($updateData);
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log('排序', '设置公告排序，id：' . $data['id'] . '，排序：' . $data['sort']);
            return [RESULT_SUCCESS, '设置成功', $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 获取公告统计数据
     */
    public function getAnnouncementStatistics($data = [])
    {
        $result = static::$commonAnnouncementLogic->getAnnouncementStatistics($data);
        
        return $result ?? [];
    }
    
    /**
     * 导出公告列表
     */
    public function exportAnnouncementList($where = [], $field = true, $order = 'sort DESC, publish_time DESC, id DESC')
    {
        $list = $this->getAnnouncementListForAdmin($where, $field, $order, false);
        
        $titles = "公告标题,公告类型,状态,排序,发布时间,创建时间";
        $keys   = "title,type_text,status_text,sort,publish_time,create_time";
        
        action_log('导出', '导出公告列表');
        
        export_excel($titles, $keys, $list, '公告列表');
    }
    
}